package servlet;

import com.google.gson.Gson;
import com.ly.common.bean.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(name = "UploadServlet", value = "/upload.do")

@MultipartConfig
public class UploadServlet extends HttpServlet {
    private SsqBiz biz = new SsqBiz();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        Part datafile = request.getPart("datafile");
        String filename = datafile.getSubmittedFileName();

        datafile.write("d:/" + filename);

        biz.save("d:/" + filename);

        Result result = new Result(1, "文件上传成功!", null);

        response.setContentType("application/json;charset=utf-8");

        String json = new Gson().toJson(result);

        response.getWriter().print(json);
    }
}
